--[[
	
	Name: Ultimate Blacklist
	By: Crypton

]]
UBL_ADDON_NAME = "Ultimate Blacklist";
UBL_VER_NUM = 0.6;
UBL_VER = "v."..UBL_VER_NUM.." Beta";
UBL_TITLE = UBL_ADDON_NAME.." "..UBL_VER;
UBL_DEFAULT_PBL = {};
UBL_DEFAULT_CHAT = {};
UBL_MESSAGE_TEMP = {};
UBL_CURK_SELECTED = 0;
UBL_CURP_SELECTED = 0;
UBL_CURCP_SELECTED = 0;
UBL_WAIT = 1.5;
UBL_SHOW = false;
UBL_POP_FLAG = false;
UBL_FLAG_RESET = false;
UBL_FLAG_WHISPER = true;
UBL_CONFIG_LOADED = false;
UBL_PATH = "Interface/AddOns/UBL/";
UBL_LANG_PATH = UBL_PATH.."local/";
UBL_DEFAULT_CONFIG = {
	["version"] = UBL_VER_NUM,
	["blmessage"] = "You have just been blacklisted",
	["decaydays"] = 3,
	["checkboxes"] = {
		[1] = true,
		[2] = false,
		[3] = false,
		[4] = false,
		[5] = false,
		[6] = false,
		[7] = false,
		[8] = false,
	},
};
UBL_LOCAL = GetLanguage();

if (UBL_LOCAL == "ENUS" or UBL_LOCAL == "ENEU") then
	UBL_LANG_FILE = "en";
elseif (AQB_LOCAL == "DE") then
	UBL_LANG_FILE = "de";
else
	UBL_LANG_FILE = "en";
	DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r Your language is not fully localized. If you would like to help by translating, please send crypton a private message on curse.com");
end

function UBLFileLoader(UBL_FPATH, UBL_FNAME)
	local func, err = loadfile(UBL_FPATH..UBL_FNAME);
	if (err) then
		DEFAULT_CHAT_FRAME:AddMessage(UBL_ADDON_NAME.." ERROR: "..UBL_FNAME.." : "..err);
		return false;
	end
	dofile(UBL_FPATH..UBL_FNAME);
	return true;
end

UBLFileLoader(UBL_LANG_PATH, "ubl_"..UBL_LANG_FILE..".lua");

function UBL_FirstTimeTS()
	local UBL_OLD_PLAYER_LIST = UBL_PLAYER_LIST;
	local UBL_ADD_TIMESTAMP = os.time();
	UBL_PLAYER_LIST = {};
	local UBL_TMP;
	local AA, BB;
	for AA, BB in pairs(UBL_OLD_PLAYER_LIST) do
		UBL_TMP = {
			["name"] = BB,
			["ts"] = UBL_ADD_TIMESTAMP,
		};
		table.insert(UBL_PLAYER_LIST, UBL_TMP);
		SaveVariables("UBL_PLAYER_LIST");
	end
	return;
end

function UBL_Check_Config()
	if (not UBL_CONFIG["version"]) then
		UBL_CONFIG = UBL_DEFAULT_CONFIG;
		SaveVariables("UBL_CONFIG");
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_UPDATED);
	elseif (UBL_CONFIG["version"] < 0.6) then
		if (UBL_CONFIG["version"] < 0.4) then
			UBL_CONFIG = UBL_DEFAULT_CONFIG;
			SaveVariables("UBL_CONFIG");
		else
			local i;
			local countsettings = table.getn(UBL_DEFAULT_CONFIG["checkboxes"]);
			UBL_CONFIG["version"] = UBL_VER_NUM;
			UBL_CONFIG["decaydays"] = UBL_DEFAULT_CONFIG["decaydays"];
			for i = 1, countsettings do
				if (not UBL_CONFIG["checkboxes"][i]) then
					UBL_CONFIG["checkboxes"][i] = UBL_DEFAULT_CONFIG["checkboxes"][i];
				end
			end
			SaveVariables("UBL_CONFIG");
		end
		UBL_FirstTimeTS();
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_UPDATED);
	end
	UBL_CONFIG_LOADED = true;
	return;
end

function UBL_ClearDefaultBlacklist()
	local igblcount = GetFriendCount("BadFriend");
	if (igblcount > 0) then
		local i = igblcount;
		repeat
			if (i < 1) then
				break;
			end
			local ublname, ubllevel, ublclass, ublloc, ublonline, ublstatus, ublnote = GetFriendInfo("BadFriend", i);
			DelFriend("BadFriend", ublname);
			i = i - 1;
		until i < 1;
	end
	if (igblcount > 0) then
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_DBLCLR);
	end
end

function UBL_GetDefaultChatStatus()
	local ichat;
	local chatframe;
	local curSay;
	local curZone;
	local curParty;
	local curRaid;
	local curWhisper;
	for ichat = 1, NUM_CHAT_WINDOWS do
		chatframe = getglobal("ChatFrame"..ichat);
		curSay = ChatFrame_CheckedMessageGroup(chatframe, "SAY");
		curZone = ChatFrame_CheckedMessageGroup(chatframe, "ZONE");
		curParty = ChatFrame_CheckedMessageGroup(chatframe, "PARTY");
		curRaid = ChatFrame_CheckedMessageGroup(chatframe, "RAID");
		curYell = ChatFrame_CheckedMessageGroup(chatframe, "YELL");
		curWhisper = ChatFrame_CheckedMessageGroup(chatframe, "WHISPER");
		UBL_DEFAULT_CHAT[ichat] = {
			["SAY"] = curSay,
			["ZONE"] = curZone,
			["YELL"] = curYell,
			["PARTY"] = curParty,
			["RAID"] = curRaid,
			["WHISPER"] = curWhisper,
		};
	end
	return;
end

function UBL_CHAT_SET(UBL_CUR_CHAN)
	local ichat;
	local chatframe;
	for ichat = 1, NUM_CHAT_WINDOWS do
		chatframe = getglobal("ChatFrame"..ichat);
		ChatFrame_RemoveMessageGroup(chatframe, UBL_CUR_CHAN);
	end
	UBL_FLAG_RESET = true;
	return;
end

function UBL_RESET_CHAT()
	local ichat;
	local countset = table.getn(UBL_DEFAULT_CHAT);
	local chatframe;
	for ichat = 1, countset do
		chatframe = getglobal("ChatFrame"..ichat);
		if (UBL_DEFAULT_CHAT[ichat]["SAY"]) then
			ChatFrame_AddMessageGroup(chatframe, "SAY");
		else
			ChatFrame_RemoveMessageGroup(chatframe, "SAY");
		end
		if (UBL_DEFAULT_CHAT[ichat]["ZONE"]) then
			ChatFrame_AddMessageGroup(chatframe, "ZONE");
		else
			ChatFrame_RemoveMessageGroup(chatframe, "ZONE");
		end
		if (UBL_DEFAULT_CHAT[ichat]["YELL"]) then
			ChatFrame_AddMessageGroup(chatframe, "YELL");
		else
			ChatFrame_RemoveMessageGroup(chatframe, "YELL");
		end
		if (UBL_DEFAULT_CHAT[ichat]["PARTY"]) then
			ChatFrame_AddMessageGroup(chatframe, "PARTY");
		else
			ChatFrame_RemoveMessageGroup(chatframe, "PARTY");
		end
		if (UBL_DEFAULT_CHAT[ichat]["RAID"]) then
			ChatFrame_AddMessageGroup(chatframe, "RAID");
		else
			ChatFrame_RemoveMessageGroup(chatframe, "RAID");
		end
		if (UBL_DEFAULT_CHAT[ichat]["WHISPER"]) then
			ChatFrame_AddMessageGroup(chatframe, "WHISPER");
		else
			ChatFrame_RemoveMessageGroup(chatframe, "WHISPER");
		end
	end
	return;
end

function UBL_CheckBox_Onclick(this)
	local chkbx = string.gsub(this, "UBLConfigFrameCheckbox", "");
	chkbx = tonumber(chkbx);
	local chkbxlang;
	if (chkbx == 1) then
		chkbxlang = "UBL";
	else
		chkbxlang = getglobal("UBL_LANG_CHKBOX"..chkbx);
	end
	if (UBL_CONFIG["checkboxes"][chkbx]) then
		UBL_CONFIG["checkboxes"][chkbx] = false;
		if (UBLMainFrame:IsVisible()) then
			DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..chkbxlang.." "..UBL_LANG_HASBEEN.." ".."|cFFff0707"..UBL_LANG_DISABLED.."|r");
		end
	else
		UBL_CONFIG["checkboxes"][chkbx] = true;
		if (UBLMainFrame:IsVisible()) then
			DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..chkbxlang.." "..UBL_LANG_HASBEEN.." ".."|cFF00ff24"..UBL_LANG_ENABLED.."|r");
		end
	end
	SaveVariables("UBL_CONFIG");
end

function UBLMainFrame_CommandHandler(this, cmd)
	if (UBL_SHOW) then
		UBL_SHOW = false;
		UBLMainFrame:Hide();
	else
		UBL_SHOW = true;
		UBLMainFrame:Show();
	end
end

function UBLMainFrame_Toggle()
	UBLMainFrame_CommandHandler(false, false);
end

function UBL_ScrollButtonCTRL(this, cval)
	local scrollbar = getglobal(this);
	local svval = scrollbar:GetValue();
	local tablecount;
	if (string.find(this, "Keyword")) then
		tablecount = table.getn(UBL_KEYWORD_LIST);
	elseif (string.find(this, "CustomPlayer")) then
		tablecount = table.getn(UBL_CPLAYER_LIST);
	else
		tablecount = table.getn(UBL_PLAYER_LIST);
	end
	if (svval == cval) then
		getglobal(this.."ScrollDownButton"):Disable();
	else
		getglobal(this.."ScrollDownButton"):Enable();
	end
	if (svval > 0) then
		getglobal(this.."ScrollUpButton"):Enable();
	else
		getglobal(this.."ScrollUpButton"):Disable();
	end
	if (svval < cval) then
		getglobal(this.."ScrollDownButton"):Enable();
	else
		getglobal(this.."ScrollDownButton"):Disable();
	end
	if (tablecount <= 8) then
		getglobal(this.."ScrollUpButton"):Disable();
		getglobal(this.."ScrollDownButton"):Disable();
	end
end

function UBL_UpdateList(scroller)
	local i;
	local btname;
	local tablecount;
	local scrollval;
	local temptable;
	local x = 9;
	local y = 11;
	if (string.find(scroller, "Keyword")) then
		btname = "UBLMainFrameKeywordScrollbarButton_";
		tablecount = table.getn(UBL_KEYWORD_LIST);
		temptable = UBL_KEYWORD_LIST;
		scrollval = UBLMainFrameKeywordScrollbarList_ScrollBar:GetValue() + 1;
	elseif (string.find(scroller, "CustomPlayer")) then
		btname = "UBLMainFrameCustomPlayerScrollbarButton_";
		tablecount = table.getn(UBL_CPLAYER_LIST);
		temptable = UBL_CPLAYER_LIST;
		scrollval = UBLMainFrameCustomPlayerScrollbarList_ScrollBar:GetValue() + 1;
	else
		btname = "UBLMainFramePlayerScrollbarButton_";
		tablecount = table.getn(UBL_PLAYER_LIST);
		temptable = UBL_PLAYER_LIST;
		scrollval = UBLMainFramePlayerScrollbarList_ScrollBar:GetValue() + 1;
	end
	for i = 1, 8 do
		btn = getglobal(btname..i);
		btn:Hide();
		btn:UnlockHighlight();
	end
	for i = 1, 8 do
		if (temptable[scrollval]) then
			btn = getglobal(btname..i);
			btn:ClearAllAnchors();
			btn.idx = scrollval;
			if (string.find(scroller, "Keyword")) then
				btn.text = temptable[scrollval];
				btn:SetAnchor("TOPLEFT", "TOPLEFT", "UBLMainFrameKeywordScrollbar", x, (21 * i) + y);
				btn:SetText(temptable[scrollval]);
				btn:Show();
				scrollval = scrollval + 1;
			elseif (string.find(scroller, "CustomPlayer")) then
				btn.text = temptable[scrollval];
				btn:SetAnchor("TOPLEFT", "TOPLEFT", "UBLMainFrameCustomPlayerScrollbar", x, (21 * i) + y);
				btn:SetText(temptable[scrollval]);
				btn:Show();
				scrollval = scrollval + 1;
			else
				btn.text = temptable[scrollval]["name"];
				btn:SetAnchor("TOPLEFT", "TOPLEFT", "UBLMainFramePlayerScrollbar", x, (21 * i) + y);
				btn:SetText(temptable[scrollval]["name"]);
				btn:Show();
				scrollval = scrollval + 1;
			end
		else
			break;
		end
	end
	if (tablecount > 8) then
		if (string.find(scroller, "Keyword")) then
			UBLMainFrameKeywordScrollbarList_ScrollBar:SetMaxValue(tablecount - 8);
			UBL_ScrollButtonCTRL("UBLMainFrameKeywordScrollbarList_ScrollBar", tablecount - 8);
		elseif (string.find(scroller, "CustomPlayer")) then
			UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetMaxValue(tablecount - 8);
			UBL_ScrollButtonCTRL("UBLMainFrameCustomPlayerScrollbarList_ScrollBar", tablecount - 8);
		else
			UBLMainFramePlayerScrollbarList_ScrollBar:SetMaxValue(tablecount - 8);
			UBL_ScrollButtonCTRL("UBLMainFramePlayerScrollbarList_ScrollBar", tablecount - 8);
		end
	else
		if (string.find(scroller, "Keyword")) then
			UBLMainFrameKeywordScrollbarList_ScrollBar:SetMaxValue(0);
			UBL_ScrollButtonCTRL("UBLMainFrameKeywordScrollbarList_ScrollBar", tablecount);
		elseif (string.find(scroller, "CustomPlayer")) then
			UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetMaxValue(0);
			UBL_ScrollButtonCTRL("UBLMainFrameCustomPlayerScrollbarList_ScrollBar", tablecount);
		else
			UBLMainFramePlayerScrollbarList_ScrollBar:SetMaxValue(0);
			UBL_ScrollButtonCTRL("UBLMainFramePlayerScrollbarList_ScrollBar", tablecount);
		end
	end
end

function UBL_Scrollbar_OnValueChanged(this, arg1)
	UBL_UpdateList(this);
end

function UBL_Scrollbar_OnMouseWheel(this, delta)
	if (delta > 0) then
		this:SetValue(this:GetValue() - 1);
	else
		this:SetValue(this:GetValue() + 1);
	end
end

function UBL_ListItem_OnClick(this)
	local btn;
	local val;
	local i;
	local flagtemp = false;
	if (string.find(this, "Keyword")) then
		val = string.gsub(this, "UBLMainFrameKeywordScrollbarButton_", "");
		val = tonumber(val);
		UBL_CURK_SELECTED = val;
		for i = 1, 8 do
			btn = getglobal("UBLMainFrameKeywordScrollbarButton_"..i);
			btn:UnlockHighlight();
		end
	elseif (string.find(this, "CustomPlayer")) then
		val = string.gsub(this, "UBLMainFrameCustomPlayerScrollbarButton_", "");
		val = tonumber(val);
		UBL_CURCP_SELECTED = val;
		for i = 1, 8 do
			btn = getglobal("UBLMainFrameCustomPlayerScrollbarButton_"..i);
			btn:UnlockHighlight();
		end
	else
		flagtemp = true;
		val = string.gsub(this, "UBLMainFramePlayerScrollbarButton_", "");
		val = tonumber(val);
		UBL_CURP_SELECTED = val;
		for i = 1, 8 do
			btn = getglobal("UBLMainFramePlayerScrollbarButton_"..i);
			btn:UnlockHighlight();
		end
	end
	btn = getglobal(this);
	btn:LockHighlight();
	if (flagtemp) then
		local countmsgtemp = table.getn(UBL_MESSAGE_TEMP);
		for i = 1, countmsgtemp do
			if (UBL_MESSAGE_TEMP[i]["name"] == btn.text) then
				DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME.." "..UBL_LANG_BADMSG..":|r "..UBL_MESSAGE_TEMP[i]["msg"]);
				break;
			end
		end
	end
end

function UBL_Update_Keywords()
	local countkeywords = table.getn(UBL_KEYWORD_LIST);
	local i;
	local x = 9;
	local y = 18;
	local ublkmax;
	UBL_CURK_SELECTED = 0;
	if (countkeywords > 0) then
		if (countkeywords > 8) then
			ublkmax = 8;
		else
			ublkmax = countkeywords;
		end
		for i = 1, 8 do
			local btn = getglobal("UBLMainFrameKeywordScrollbarButton_"..i);
			btn:UnlockHighlight();
			btn:Hide();
		end
		for i = 1, ublkmax do
			local btn = getglobal("UBLMainFrameKeywordScrollbarButton_"..i);
			btn:ClearAllAnchors();
			btn:SetAnchor("LEFT", "TOPLEFT", "UBLMainFrameKeywordScrollbar", x, (21 * i) + y);
			btn:SetText(UBL_KEYWORD_LIST[i]);
			btn.idx = i;
			btn.text = UBL_KEYWORD_LIST[i];
			btn:Show();
		end
	end
	if (countkeywords > 8) then
		UBLMainFrameKeywordScrollbarList_ScrollBar:SetMaxValue(countkeywords - 8);
		UBL_ScrollButtonCTRL("UBLMainFrameKeywordScrollbarList_ScrollBar", countkeywords - 8);
	else
		UBLMainFrameKeywordScrollbarList_ScrollBar:SetMaxValue(0);
		UBL_ScrollButtonCTRL("UBLMainFrameKeywordScrollbarList_ScrollBar", countkeywords);
	end
	return;
end

function UBL_Update_Players()
	local countplayers = table.getn(UBL_PLAYER_LIST);
	local i;
	local x = 9;
	local y = 18;
	local ublpmax;
	UBL_CURP_SELECTED = 0;
	if (countplayers > 0) then
		if (countplayers > 8) then
			ublpmax = 8;
		else
			ublpmax = countplayers;
		end
		for i = 1, 8 do
			local btn = getglobal("UBLMainFramePlayerScrollbarButton_"..i);
			btn:UnlockHighlight();
			btn:Hide();
		end
		for i = 1, ublpmax do
			local btn = getglobal("UBLMainFramePlayerScrollbarButton_"..i);
			btn:ClearAllAnchors();
			btn:SetAnchor("LEFT", "TOPLEFT", "UBLMainFramePlayerScrollbar", x, (21 * i) + y);
			btn:SetText(UBL_PLAYER_LIST[i]["name"]);
			btn.idx = i;
			btn.text = UBL_PLAYER_LIST[i]["name"];
			btn:Show();
		end
	end
	if (countplayers > 8) then
		UBLMainFramePlayerScrollbarList_ScrollBar:SetMaxValue(countplayers - 8);
		UBL_ScrollButtonCTRL("UBLMainFramePlayerScrollbarList_ScrollBar", countplayers - 8);
	else
		UBLMainFramePlayerScrollbarList_ScrollBar:SetMaxValue(0);
		UBL_ScrollButtonCTRL("UBLMainFramePlayerScrollbarList_ScrollBar", countplayers);
	end
	return;
end

function UBL_Update_Custom_Players()
	local countplayers = table.getn(UBL_CPLAYER_LIST);
	local i;
	local x = 9;
	local y = 18;
	local ublcpmax;
	UBL_CURCP_SELECTED = 0;
	if (countplayers > 0) then
		if (countplayers > 8) then
			ublcpmax = 8;
		else
			ublcpmax = countplayers;
		end
		for i = 1, 8 do
			local btn = getglobal("UBLMainFrameCustomPlayerScrollbarButton_"..i);
			btn:UnlockHighlight();
			btn:Hide();
		end
		for i = 1, ublcpmax do
			local btn = getglobal("UBLMainFrameCustomPlayerScrollbarButton_"..i);
			btn:ClearAllAnchors();
			btn:SetAnchor("LEFT", "TOPLEFT", "UBLMainFrameCustomPlayerScrollbar", x, (21 * i) + y);
			btn:SetText(UBL_CPLAYER_LIST[i]);
			btn.idx = i;
			btn.text = UBL_CPLAYER_LIST[i];
			btn:Show();
		end
	end
	if (countplayers > 8) then
		UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetMaxValue(countplayers - 8);
		UBL_ScrollButtonCTRL("UBLMainFrameCustomPlayerScrollbarList_ScrollBar", countplayers - 8);
	else
		UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetMaxValue(0);
		UBL_ScrollButtonCTRL("UBLMainFrameCustomPlayerScrollbarList_ScrollBar", countplayers);
	end
	return;
end

function UBL_Add_Keyword()
	local UBL_CUR_KEYWORD = UBLMainFrameKEditBox:GetText();
	local countkeywords = table.getn(UBL_KEYWORD_LIST);
	local ublkwflag = false;
	local i;
	if (UBL_CUR_KEYWORD == "") then
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_INFO2);
		return;
	end
	for i = 1, countkeywords do
		if (UBL_KEYWORD_LIST[i] == UBL_CUR_KEYWORD) then
			ublkwflag = true;
			break;
		end
	end
	if (not ublkwflag) then
		table.insert(UBL_KEYWORD_LIST, UBL_CUR_KEYWORD)
		SaveVariables("UBL_KEYWORD_LIST");
		UBL_Update_Keywords();
	end
	if (UBLMainFrame:IsVisible()) then
		UBLMainFrameKEditBox:SetText("");
	end
	DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_KEYWORDADDED);
end

function UBL_Add_Player()
	local UBL_CUR_PLAYER = UBLMainFrameCPEditBox:GetText();
	local countplayers1 = table.getn(UBL_PLAYER_LIST);
	local countplayers2 = table.getn(UBL_CPLAYER_LIST);
	local ublpflag = false;
	local i;
	if (UBL_CUR_PLAYER == "") then
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_INFO2);
		return;
	end
	for i = 1, countplayers1 do
		if (UBL_PLAYER_LIST[i] == UBL_CUR_PLAYER) then
			ublpflag = true;
			break;
		end
	end
	if (ublpflag) then
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_CUR_PLAYER.." "..UBL_LANG_ALREADYON.." "..UBL_LANG_AUTO.." "..UBL_LANG_BLACKLIST);
		UBLMainFrameCPEditBox:SetText("");
		return;
	end
	for i = 1, countplayers2 do
		if (UBL_CPLAYER_LIST[i] == UBL_CUR_PLAYER) then
			ublpflag = true;
			break;
		end
	end
	if (not ublpflag) then
		table.insert(UBL_CPLAYER_LIST, UBL_CUR_PLAYER);
		SaveVariables("UBL_CPLAYER_LIST");
		UBL_Update_Custom_Players();
	end
	if (UBLMainFrame:IsVisible()) then
		UBLMainFrameCPEditBox:SetText("");
	end
	DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_PLAYERADDED);
end

function UBL_AUTO_ADD_PLAYER(UBL_CUR_PLAYER, UBL_CUR_STRING)
	local countplayers1 = table.getn(UBL_PLAYER_LIST);
	local countplayers2 = table.getn(UBL_CPLAYER_LIST);
	local countplayers3 = table.getn(UBL_MESSAGE_TEMP);
	local ublpflag = false;
	local ublcpflag = false;
	local ublflagtemp = false;
	local ubltmparr = {
		["name"] = UBL_CUR_PLAYER,
		["msg"] = UBL_CUR_STRING,
	};
	local ubltmparr2 = {
		["name"] = UBL_CUR_PLAYER,
		["ts"] = os.time(),
	};
	local i;
	for i = 1, countplayers1 do
		if (UBL_PLAYER_LIST[i]["name"] == UBL_CUR_PLAYER) then
			ublpflag = true;
			break;
		end
	end
	for i = 1, countplayers2 do
		if (UBL_CPLAYER_LIST[i] == UBL_CUR_PLAYER) then
			ublcpflag = true;
			break;
		end
	end
	if (not ublpflag and not ublcpflag) then
		table.insert(UBL_PLAYER_LIST, ubltmparr2);
		SaveVariables("UBL_PLAYER_LIST");
		if (countplayers3 > 0) then
			for i = 1, countplayers3 do
				if (UBL_MESSAGE_TEMP[i]["name"] == UBL_CUR_PLAYER) then
					ublflagtemp = true;
				end
			end
		end
		if (not ublflagtemp) then
			table.insert(UBL_MESSAGE_TEMP, ubltmparr);
		end
		if (UBLMainFrame:IsVisible()) then
			UBL_Update_Players();
		end
		if (UBL_CONFIG["checkboxes"][2]) then
			DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r |cFFff0707"..UBL_CUR_PLAYER.."|r "..UBL_LANG_ADDED);
			if (UBL_CONFIG["checkboxes"][3]) then
				UBL_FLAG_WHISPER = true;
				SendChatMessage(UBL_ADDON_NAME..": "..UBL_CONFIG["blmessage"], "WHISPER", 0, UBL_CUR_PLAYER);
			end
		end
	end
	return;
end

function UBL_Delete_Keyword()
	local i;
	local j, k;
	local tmparr = {};
	if (UBL_CURK_SELECTED > 0) then
		for i = 1, 8 do
			local btn = getglobal("UBLMainFrameKeywordScrollbarButton_"..i);
			btn:UnlockHighlight();
			btn:Hide();
		end
		local btn = getglobal("UBLMainFrameKeywordScrollbarButton_"..UBL_CURK_SELECTED);
		local itemtxt = btn.text;
		for j, k in pairs(UBL_KEYWORD_LIST) do
			if (k ~= itemtxt) then
				table.insert(tmparr, k);
			end
		end
		UBL_KEYWORD_LIST = tmparr;
		SaveVariables("UBL_KEYWORD_LIST");
		if (UBLMainFrame:IsVisible()) then
			UBL_Update_Keywords();
		end
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_LISTUPDATED);
	else
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_INFO3);
	end
end

function UBL_Delete_Player()
	local i;
	local j, k;
	local tmparr = {};
	if (UBL_CURP_SELECTED > 0) then
		for i = 1, 8 do
			local btn = getglobal("UBLMainFramePlayerScrollbarButton_"..i);
			btn:UnlockHighlight();
			btn:Hide();
		end
		local btn = getglobal("UBLMainFramePlayerScrollbarButton_"..UBL_CURP_SELECTED);
		local itemtxt = btn.text;
		for j, k in pairs(UBL_PLAYER_LIST) do
			if (k["name"] ~= itemtxt) then
				table.insert(tmparr, k);
			end
		end
		UBL_PLAYER_LIST = tmparr;
		SaveVariables("UBL_PLAYER_LIST");
		if (UBLMainFrame:IsVisible()) then
			UBL_Update_Players();
		end
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_LISTUPDATED);
	else
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_INFO3);
	end
end

function UBL_Delete_Custom_Player()
	local i;
	local j, k;
	local tmparr = {};
	if (UBL_CURCP_SELECTED > 0) then
		for i = 1, 8 do
			local btn = getglobal("UBLMainFrameCustomPlayerScrollbarButton_"..i);
			btn:UnlockHighlight();
			btn:Hide();
		end
		local btn = getglobal("UBLMainFrameCustomPlayerScrollbarButton_"..UBL_CURCP_SELECTED);
		local itemtxt = btn.text;
		for j, k in pairs(UBL_CPLAYER_LIST) do
			if (k ~= itemtxt) then
				table.insert(tmparr, k);
			end
		end
		UBL_CPLAYER_LIST = tmparr;
		SaveVariables("UBL_CPLAYER_LIST");
		if (UBLMainFrame:IsVisible()) then
			UBL_Update_Custom_Players();
		end
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_LISTUPDATED);
	else
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_INFO3);
	end
end

function UBL_FilterCheck(UBL_PLAYER, UBL_STRING, UBL_TYPE)
	local i;
	local UBL_BLOCK_CHAN;
	local UBL_NEW_STRING = string.gsub(UBL_STRING, "%s+", "");
	UBL_NEW_STRING = string.gsub(UBL_NEW_STRING, "%p+", "");
	UBL_NEW_STRING = string.gsub(UBL_NEW_STRING, "%c+", "");
	UBL_NEW_STRING = string.lower(UBL_NEW_STRING);
	local UBL_PLAYER_FLAG = false;
	local UBL_COUNT_KLIST = table.getn(UBL_KEYWORD_LIST);
	local UBL_COUNT_PLIST = table.getn(UBL_PLAYER_LIST);
	local UBL_COUNT_CPLIST = table.getn(UBL_CPLAYER_LIST);
	for i = 1, UBL_COUNT_CPLIST do
		if (UBL_CPLAYER_LIST[i] == UBL_PLAYER) then
			UBL_PLAYER_FLAG = true;
			break;
		end
	end
	if (not UBL_PLAYER_FLAG) then
		for i = 1, UBL_COUNT_PLIST do
			if (UBL_PLAYER_LIST[i] == UBL_PLAYER) then
				UBL_PLAYER_FLAG = true;
				break;
			end
		end
	end
	if (not UBL_PLAYER_FLAG) then
		for i = 1, UBL_COUNT_KLIST do
			local UBL_CHK_KEYWORD = string.gsub(UBL_KEYWORD_LIST[i], "%s+", "");
-- Commented out for now, could prevent pattern searching...
--			UBL_CHK_KEYWORD = string.gsub(UBL_CHK_KEYWORD, "%p+", "");
--			UBL_CHK_KEYWORD = string.gsub(UBL_CHK_KEYWORD, "%c+", "");
			UBL_CHK_KEYWORD = string.lower(UBL_CHK_KEYWORD);
			if (string.match(UBL_NEW_STRING, UBL_CHK_KEYWORD)) then
				UBL_PLAYER_FLAG = true;
				break;
			end
		end
	end
	if (UBL_TYPE == "CHAT_MSG_WHISPER" or UBL_TYPE == "CHAT_MSG_WHISPER_OFFLINE") then
		UBL_BLOCK_CHAN = "WHISPER";
		if (UBL_PLAYER_FLAG) then
			UBL_POP_FLAG = true;
		end
	elseif (UBL_TYPE == "CHAT_MSG_ZONE") then
		UBL_BLOCK_CHAN = "ZONE";
	elseif (UBL_TYPE == "CHAT_MSG_YELL") then
		UBL_BLOCK_CHAN = "YELL";
	elseif (UBL_TYPE == "CHAT_MSG_PARTY") then
		UBL_BLOCK_CHAN = "PARTY";
	elseif (UBL_TYPE == "CHAT_MSG_RAID") then
		UBL_BLOCK_CHAN = "RAID";
	else
		UBL_BLOCK_CHAN = "SAY";
	end
	if (UBL_PLAYER_FLAG) then
		UBL_CHAT_SET(UBL_BLOCK_CHAN);
		UBL_AUTO_ADD_PLAYER(UBL_PLAYER, UBL_STRING);
	end
	return;
end

function UBL_CheckRequest(reqtype, reqname)
	local i;
	local statbox;
	local UBL_CRPLAYER_FLAG = false;
	local UBL_CRCOUNT_PLIST = table.getn(UBL_PLAYER_LIST);
	local UBL_CRCOUNT_CPLIST = table.getn(UBL_CPLAYER_LIST);
	if (reqtype == "PARTY") then
		if (UBL_CONFIG["checkboxes"][5]) then
			UBL_CRPLAYER_FLAG = true;
		end
	elseif (reqtype == "DUEL") then
		if (UBL_CONFIG["checkboxes"][6]) then
			UBL_CRPLAYER_FLAG = true;
		end
	end
	if (not UBL_CRPLAYER_FLAG) then
		for i = 1, UBL_CRCOUNT_CPLIST do
			if (UBL_CPLAYER_LIST[i] == reqname) then
				UBL_CRPLAYER_FLAG = true;
				break;
			end
		end
	end
	if (not UBL_CRPLAYER_FLAG) then
		for i = 1, UBL_CRCOUNT_PLIST do
			if (UBL_PLAYER_LIST[i] == reqname) then
				UBL_CRPLAYER_FLAG = true;
				break;
			end
		end
	end
	if (UBL_CRPLAYER_FLAG) then
		if (reqtype == "PARTY") then
			statbox = "PARTY_INVITE";
		else
			statbox = "DUEL_REQUESTED";
		end
		local info = StaticPopupDialogs[statbox];
		StaticPopupDialogs[statbox].timeleft = 0;
		for i = 1, STATICPOPUP_NUMDIALOGS, 1 do
			local frame = getglobal("StaticPopup"..i);
			if (frame:IsVisible()) then
				local OnCancel = StaticPopupDialogs[statbox].OnCancel;
				if (OnCancel) then
					OnCancel(frame, "clicked");
				end
				frame:Hide();
			end
		end
	end
	return;
end

function UBL_CheckTimeStamps()
	local i;
	local z = 0;
	local countauto = table.getn(UBL_PLAYER_LIST);
	local temprem = {};
	local decay = ((60 * 60) * 24) * UBL_CONFIG["decaydays"];
	local curts = os.time();
	for i = 1, countauto do
		local tsdiff = os.difftime(curts, UBL_PLAYER_LIST[i]["ts"]);
		if (tsdiff < decay) then
			local newblar = {
				["name"] = UBL_PLAYER_LIST[i]["name"],
				["ts"] = UBL_PLAYER_LIST[i]["ts"],
			};
			table.insert(temprem, newblar);
		else
			z = z + 1;
		end
	end
	UBL_PLAYER_LIST = temprem;
	if (z > 0) then
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..string.gsub(UBL_LANG_REMOVED, "%(N%)", z));
	end
	return;
end

function UBLMainFrame_OnLoad(this)
	this:RegisterEvent("VARIABLES_LOADED");
	this:RegisterEvent("DUEL_REQUESTED");
	this:RegisterEvent("PARTY_INVITE_REQUEST");
	this:RegisterEvent("CHAT_MSG_ZONE");
	this:RegisterEvent("CHAT_MSG_YELL");
	this:RegisterEvent("CHAT_MSG_SAY");
	this:RegisterEvent("CHAT_MSG_PARTY");
	this:RegisterEvent("CHAT_MSG_RAID");
	this:RegisterEvent("CHAT_MSG_WHISPER");
	this:RegisterEvent("CHAT_MSG_WHISPER_INFORM");
	this:RegisterEvent("CHAT_MSG_WHISPER_OFFLINE");
	this:RegisterEvent("CHAT_MSN_POPUP");
	this:RegisterEvent("CHAT_MSN_OPEN");
end

function UBLMainFrame_OnEvent(this, event)
	if (event == "VARIABLES_LOADED") then
		SLASH_UBL1 = "/ubl";
		SlashCmdList["UBL"] = UBLMainFrame_CommandHandler;
		-- Begin AddonManager Support
		if AddonManager then
			AddonManager.RegisterAddon(
				"|cffFFFF00"..UBL_ADDON_NAME.."|r",
				"\n"..UBL_LANG_DESCR,
				"Interface\\AddOns\\UBL\\images\\ubl.png",
				"Social",
				UBLConfigFrame,
				"/ubl",
				UBLMiniButton,
				UBLMainFrame_Toggle,
				UBL_VER,
				"Crypton",
				UBL_Enable,
				UBL_Enable
			);
		-- End AddonManager Support
		else
			DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_TITLE.."|r "..UBL_LANG_BY.." |cFFffffffCrypton|r "..UBL_LANG_LOADED.."...");
			DEFAULT_CHAT_FRAME:AddMessage(UBL_LANG_TYPE.." |cFFffffff/ubl|r "..UBL_LANG_INFO1);
		end
		if (UBL_KEYWORD_LIST == nil) then
			UBL_KEYWORD_LIST = UBL_DEFAULT_KEYWORDS;
		end
		SaveVariables("UBL_KEYWORD_LIST");
		if (UBL_PLAYER_LIST == nil) then
			UBL_PLAYER_LIST = {};
		end
		SaveVariables("UBL_PLAYER_LIST");
		if (UBL_CPLAYER_LIST == nil) then
			UBL_CPLAYER_LIST = {};
		end
		SaveVariables("UBL_CPLAYER_LIST");
		if (UBL_CONFIG == nil) then
			UBL_CONFIG = UBL_DEFAULT_CONFIG;
		end
		SaveVariables("UBL_CONFIG");
		UBLMainFrameKeywordScrollbarList_ScrollBar:SetValueStepMode("INT");
		UBLMainFrameKeywordScrollbarList_ScrollBar:SetMaxValue(0);
		UBLMainFrameKeywordScrollbarList_ScrollBar:SetValue(0);
		UBLMainFramePlayerScrollbarList_ScrollBar:SetValueStepMode("INT");
		UBLMainFramePlayerScrollbarList_ScrollBar:SetMaxValue(0);
		UBLMainFramePlayerScrollbarList_ScrollBar:SetValue(0);
		UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetValueStepMode("INT");
		UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetMaxValue(0);
		UBLMainFrameCustomPlayerScrollbarList_ScrollBar:SetValue(0);
		UBLMainFrameKeywordScrollbarTitle:SetText(UBL_LANG_KEYWORD.." "..UBL_LANG_BLACKLIST);
		UBLMainFramePlayerScrollbarTitle:SetText(UBL_LANG_AUTO.." "..UBL_LANG_BLACKLIST);
		UBLMainFrameCustomPlayerScrollbarTitle:SetText(UBL_LANG_CPLIST.." "..UBL_LANG_BLACKLIST);
		UBLConfigFrameTitle:SetText(UBL_ADDON_NAME.." "..UBL_LANG_CONFIG);
		UBL_GetDefaultChatStatus();
		UBL_Check_Config();
		if (UBL_CONFIG["checkboxes"][4]) then
			UBL_CheckTimeStamps();
		end
	end
	if (not UBL_CONFIG_LOADED) then
		return;
	end
	if (UBL_CONFIG["checkboxes"][1]) then
		if (UnitName("player") ~= arg4) then
			if (event == "CHAT_MSG_WHISPER_INFORM") then
				return;
			end
			if (event == "CHAT_MSG_SAY") then
				local ublplayer = arg4;
				local ubltext = arg1;
				UBL_FilterCheck(ublplayer, ubltext, event);
			elseif (event == "CHAT_MSG_YELL") then
				local ublplayer = arg4;
				local ubltext = arg1;
				if (UBL_CONFIG["checkboxes"][8]) then
					UBL_FilterCheck(ublplayer, ubltext, event);
				end
			elseif (event == "CHAT_MSG_ZONE") then
				local ublplayer = arg4;
				local ubltext = arg1;
				UBL_FilterCheck(ublplayer, ubltext, event);
			elseif (event == "CHAT_MSG_PARTY") then
				local ublplayer = arg4;
				local ubltext = arg1;
				if (UBL_CONFIG["checkboxes"][7]) then
					UBL_FilterCheck(ublplayer, ubltext, event);
				end
			elseif (event == "CHAT_MSG_RAID") then
				local ublplayer = arg4;
				local ubltext = arg1;
				if (UBL_CONFIG["checkboxes"][7]) then
					UBL_FilterCheck(ublplayer, ubltext, event);
				end
			elseif (event == "CHAT_MSG_WHISPER" or event == "CHAT_MSG_WHISPER_OFFLINE") then
				local ublplayer = arg4;
				local ubltext = arg1;
				UBL_FilterCheck(ublplayer, ubltext, event);
			elseif (event == "CHAT_MSN_POPUP") then
				local ublplayer = arg4;
				if (UBL_POP_FLAG) then
					UBL_POP_FLAG = false;
					local popup = getglobal("CRG_Chat"..arg1);
					popup:Hide();
				end
			elseif (event == "CHAT_MSN_OPEN") then
				if (UBL_FLAG_WHISPER) then
					local popup = getglobal("CRG_Chat"..arg1);
					popup:Hide();
					UBL_FLAG_WHISPER = false;
				end
			elseif (event == "PARTY_INVITE_REQUEST") then
				UBL_CheckRequest("PARTY", arg1);
			elseif (event == "DUEL_REQUESTED") then
				UBL_CheckRequest("DUEL", arg1);
			end
		end
	end
end

function UBLMainFrame_OnUpdate(curtimer)
	if (not UBL_CONFIG_LOADED) then
		return;
	end
	if (UBL_CONFIG["checkboxes"][1]) then
		UBL_WAIT = UBL_WAIT - curtimer;
		if (UBL_WAIT <= 0) then
			if (UBL_FLAG_RESET) then
				UBL_FLAG_RESET = false;
				UBL_RESET_CHAT();
			end
			UBL_WAIT = 1.5;
		end
	end
end

function UBL_OnShow()
	UBL_Update_Keywords();
	UBL_Update_Players();
	UBL_Update_Custom_Players();
end

function UBL_Config_OnClick()
	if (UBLConfigFrame:IsVisible()) then
		UBLConfigFrame:Hide();
	else
		UBLConfigFrame:Show();
	end
end

function UBLCustomMessageFrameSaveButton_OnClick()
	UBL_CONFIG["blmessage"] = UBLConfigFrameCustomMSGEditBox:GetText();
	SaveVariables("UBL_CONFIG");
	DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_UPDATED);
end

function UBLCheckNum(mystrn)
	local i;
	local flagged = false;
	local searchpatterns = {
		[1] = "%a+",
		[2] = "%s+",
		[3] = "%p+",
		[4] = "%c+",
	};
	for i = 1, 4 do
		if (string.find(mystrn, searchpatterns[i])) then
			flagged = true;
			break;
		end
	end
	return flagged;
end

function UBLDecayDaysSave()
	local ubloldval = UBL_CONFIG["decaydays"];
	local ublnewval = UBLConfigFrameDecayDaysEditBox:GetText();
	local nodigit = UBLCheckNum(ublnewval);
	if (nodigit) then
		DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_NOTNUM);
	elseif (string.find(ublnewval, "%d+")) then
		ubloldval = tonumber(ubloldval);
		ublnewval = tonumber(ublnewval);
		if (ublnewval > 0) then
			if (ublnewval ~= ubloldval) then
				UBL_CONFIG["decaydays"] = ublnewval;
				SaveVariables("UBL_CONFIG");
				DEFAULT_CHAT_FRAME:AddMessage("|cFFffffff"..UBL_ADDON_NAME..":|r "..UBL_LANG_UPDATED);
			end
		end
	end
end

function UBL_Config_OnShow()
	local i;
	local chkbox;
	local chkboxtext;
	local optlang;
	local configcount = table.getn(UBL_CONFIG["checkboxes"]);
	for i = 1, configcount do
		if (i == 1) then
			optlang = "UBL "..getglobal("UBL_LANG_CHKBOX"..i);
		else
			optlang = getglobal("UBL_LANG_CHKBOX"..i);
		end
		chkbox = getglobal("UBLConfigFrameCheckbox"..i);
		chkbox:SetChecked(UBL_CONFIG["checkboxes"][i]);
		chkboxtext = getglobal("UBLConfigFrameCheckbox"..i.."Text");
		chkboxtext:SetText(optlang);
	end
	UBLConfigFrameCustomMSGEditBox:SetText(UBL_CONFIG["blmessage"]);
	UBLConfigFrameDecayDaysEditBox:SetText(UBL_CONFIG["decaydays"]);
end
